package fanshe2;

import java.lang.reflect.Field;

public class fansheCar {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException {
        String classPath ="fanshe2.Car";
        //获取反射对象
        Class<?> cls = Class.forName(classPath);//利用文件路径找到类对象\反射对象
        System.out.println("cls = " + cls);
        System.out.println("cls.getClass() = " + cls.getClass());//反射找到类的对象
        System.out.println("cls.getPackage().getName() = " + cls.getPackage().getName());//反射找到包名
        System.out.println("cls.getName() = " + cls.getName());//反射找到类的完整的名字
        //运行时创建对象实例
        Car car = (Car)cls.newInstance();
        System.out.println("car = " + car);//输出车

        Field brand = cls.getField("brand");//反射找到属性
        System.out.println("brand = " + brand);
        System.out.println("brand.get(car) = " + brand.get(car));//获取属性的值
    }
}
